package com.cloudlead.customerAction.service.impl;

import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.customerAction.service.BookingSpaceReceiptFindSer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/9/5.
 */
@Service
public class BookingSpaceReceiptFindSerImpl implements BookingSpaceReceiptFindSer {

    @Autowired
    TbBookingSpaceReceiptService tbBookingSpaceReceiptService;
    @Autowired
    TbBookingSpaceOrderService tbBookingSpaceOrderService;
    @Autowired
    TbCustomerOrderService  tbCustomerOrderService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    BaseShipService baseShipService;
    @Autowired
    BaseShipRouteService baseShipRouteService;


    @Override
    public Object BookingSpaceReceiptFindOne(String Id) {
        TbBookingSpaceReceipt bsr = tbBookingSpaceReceiptService.view(Id);//通过ID查询订舱回执
        if(bsr!=null&&bsr.getTbBookingSpaceOrder()!=null&&bsr.getTbBookingSpaceOrder().getId()!=null){//订舱回执存在 订舱单ID不为空
            TbBookingSpaceOrder bso = tbBookingSpaceOrderService.view(bsr.getTbBookingSpaceOrder().getId());//查询订舱单
                if(bso!=null) {//订舱单存在
                    if(bso.getTbCustomerOrder()!=null&&bso.getTbCustomerOrder().getId()!=null){//订单ID不为空
                        TbCustomerOrder co = tbCustomerOrderService.view(bso.getTbCustomerOrder().getId());//查询订单
                        bso.setTbCustomerOrder(co);//替换订单
                    }
                    bsr.setTbBookingSpaceOrder(bso);//替换订舱单
                }
        }
        return bsr;
    }

    @Override
    public List<Object> BookingSpaceReceiptFindAll(String bookingSpaceOrderId) {
        //查询条件
        TbBookingSpaceReceipt bsr = new TbBookingSpaceReceipt();
            TbBookingSpaceOrder bso =new TbBookingSpaceOrder();//订舱单ID
            bso.setId(bookingSpaceOrderId);
        bsr.setTbBookingSpaceOrder(bso);
        bsr.setFlag("0");//0表示未删除
        List<TbBookingSpaceReceipt> lbsr = tbBookingSpaceReceiptService.list(bsr,null);//查询订舱回执
        List<Object> lo = new ArrayList<Object>();
        for(Iterator<TbBookingSpaceReceipt> it = lbsr.iterator();it.hasNext();) {//遍历
            TbBookingSpaceReceipt bsr1 =it.next();
            if(bsr1.getTbBookingSpaceOrder()!=null&&bsr1.getTbBookingSpaceOrder().getTbCustomerOrder()!=null){
                if(bsr1.getTbBookingSpaceOrder().getTbCustomerOrder().getId()!=null){//订单ID是否为空
                    //查询订单信息
                    TbCustomerOrder co =  tbCustomerOrderService.view(bsr1.getTbBookingSpaceOrder().getTbCustomerOrder().getId());
                    if(co!=null){
                        TbBookingSpaceOrder bso1= new TbBookingSpaceOrder();
                            BaseVoyage bv=co.getBaseVoya();
                                if(bv!=null){//航次不为空
                                    if(bv.getBaseCompany()!=null&&bv.getBaseCompany().getId()!=null){//航次公司
                                        BaseCompany bc=baseCompanyService.view(bv.getBaseCompany().getId());
                                        if(bc!=null)
                                            bv.setBaseCompany(bc);//替换公司
                                    }
                                    if(bv.getBaseShip()!=null&&bv.getBaseShip().getId()!=null){//船舶不为空
                                        BaseShip bs = baseShipService.view(bv.getBaseShip().getId());
                                        if(bs!=null)
                                            bv.setBaseShip(bs);//替换船舶
                                    }
/*                                    if(bv.getBaseShipRoute()!=null&&bv.getBaseShipRoute().getId()!=null){//航线不为空
                                        BaseShipRoute bsrx = baseShipRouteService.view(bv.getBaseShipRoute().getId());
                                        if(bsrx!=null)
                                            bv.setBaseShipRoute(bsrx);
                                    }*/

                                }
                                co.setBaseVoya(bv);//替换航次
                            bso1.setTbCustomerOrder(co);//替换订单
                        bsr1.setTbBookingSpaceOrder(bso1);//替换订舱单
                    }
                }
            }
            lo.add(bsr1);
        }
        return lo;
    }
}
